﻿namespace ApiTemplate.WebApi
{
    /// <summary>
    /// 设置swaggerUI
    /// </summary>
    public static class SwaggerSetup
    {
        /// <summary>
        /// 设置swaggerUI
        /// </summary>
        /// <param name="services"></param>
        /// <param name="appSetting"></param>
        public static void AddSwaggerSetup(this IServiceCollection services, AppSetting appSetting)
        {
            //添加性能监控
            if (appSetting.EnableMiniProfiler)
            {
                services.AddMiniProfiler(options =>
                {
                    options.RouteBasePath = "/profiler";
                    options.PopupRenderPosition = StackExchange.Profiling.RenderPosition.Left;
                    options.PopupShowTimeWithChildren = true;
                });
            }

            if (appSetting.SwaggerUI != SwaggerUI.Disable)
            {
                //集成swagger文档
                services.AddSwaggerGen(options =>
                {
                    options.SwaggerDoc("v1", new OpenApiInfo() { Title = "WebAPI", Version = "v1" });

                    #region swagger中集成简单的用户权限
                    options.AddSecurityDefinition("Token", new OpenApiSecurityScheme()
                    {
                        Description = "在这里输入Token，可以自动添加到header中",
                        Name = "Token",
                        In = ParameterLocation.Header,
                        Type = SecuritySchemeType.ApiKey
                    });

                    options.AddSecurityRequirement(new OpenApiSecurityRequirement
                    {
                        {
                            new OpenApiSecurityScheme
                            {
                                Reference = new OpenApiReference {
                                    Type = ReferenceType.SecurityScheme,
                                    Id = "Token"
                                }
                            },
                            Array.Empty<string>()
                        }
                    });
                    #endregion

                    options.CustomSchemaIds(type => type.FullName);
                    options.IncludeXmlComments($"{AppDomain.CurrentDomain.BaseDirectory}/ApiTemplate.WebApi.xml");
                    options.IncludeXmlComments($"{AppDomain.CurrentDomain.BaseDirectory}/ApiTemplate.Entity.xml");
                });
            }
        }

        /// <summary>
        /// 启用swagger
        /// </summary>
        /// <param name="app"></param>
        /// <param name="env"></param>
        /// <param name="appSetting"></param>
        public static void UseSwaggerDoc(this IApplicationBuilder app, IWebHostEnvironment env, AppSetting appSetting)
        {

            switch (appSetting.SwaggerUI)
            {
                case SwaggerUI.Disable:
                    break;
                case SwaggerUI.Enable:
                    //启用swagger
                    app.UseSwagger();
                    break;
                case SwaggerUI.Dev:
                    if (env.IsDevelopment())
                    {
                        app.UseSwagger();
                    }
                    break;
            }


            app.UseSwaggerUI(c =>
            {
                c.SwaggerEndpoint("/swagger/v1/swagger.json", "WebAPI");
                //这里是启动页面直接进入swagger文档
                c.RoutePrefix = "";
            });
            //启用性能监控器
            if (appSetting.EnableMiniProfiler)
            {
                app.UseMiniProfiler();
            }
        }
    }
}
